// pages/classify/classify.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentOne: '',
    currentTwo: '',
    winH: 0,
    tab1H: 0,
    tab2H: 0,
    audios: [],
    pageNum: 1,
    
    isOver: false,
    classifyArr: [],
    typeid: null,
    appList: [],
    isfirst:true,
    fid: 0
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'...options')
    this.getdomwidth('.tabOne').then(() => {
      this.getdomwidth('.tabTwo').then(() => {
        this.setData({
          winH: wx.getSystemInfoSync().windowHeight - this.data.tab1H - this.data.tab2H
        })
      })
    })
    this.setData({
      currentTwo:parseInt(options.typeid),
      currentOne:options.appid,
      fid: app.globalData.fid
    })
    this.audioApps();
  },


  changeTabOne: function (e) {
    if(e.detail.name == this.data.currentOne){
      return false;
    }
    this.setData({
      currentOne: e.detail.name,
      audios: [],
      pageNum: 1,
      isOver: false,
      classifyArr: [],
      typeid: null,
      isfirst:false
    });
    this.audioCates(e.detail.name);
  },

  changeTabTwo: function (e) {
    if(this.data.currentTwo == e.detail.name){
      return false;
    }
    console.log(e.detail.name)
    this.setData({
      currentTwo: e.detail.name,
      audios: [],
      pageNum: 1,
      isOver: false
    })
    this.audios(this.data.currentOne, this.data.currentTwo);
  },

  getMoreData: function () {
    this.audios(this.data.currentOne, this.data.currentTwo)
  },


  setscrollHeight:function(){
    
  },


  getdomwidth: function (cl) {
    var _this = this;
    return new Promise(resolve => {
      wx.createSelectorQuery().selectAll(cl).boundingClientRect(function (rect) {
        if (cl == '.tabOne') {
          _this.setData({
            tab1H: rect[0].height
          })
        } else {
          _this.setData({
            tab2H: rect[0].height
          })
        }
        resolve()
      }).exec();

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    let url = util.getCurrentPageUrlWithArgs();
    if (url.indexOf('?') > -1) {
      url += '&fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    } else {
      url += '?fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    }
    if (options.from == "menu") {
      return {
        title: app.globalData.site.name,
        path: url
      }
    }
  },

  onShareTimeline:function(options){
    let query ='';
    let url = util.getCurrentPageUrlWithArgs();
    if (url.indexOf('?') > -1) {
      query = '&fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    } else {
      query = 'fid=' + app.globalData.fid + '&fidEnc=' + app.globalData.fidEnc;
    }
    query += '&typeid='+this.data.currentTwo+'&appid='+this.data.currentOne
      return {
        title: app.globalData.site.name,
        query: query
      }
  },
  //获取单位app应用
  // audioApps: function () {
  //   var that = this;
  //   wx.request({
  //     url: 'https://wxgt.chaoxing.com/wechat/news/apps',
  //     data: {
  //       wfwfid: app.globalData.fid
  //     },
  //     success: function (res) {
  //       that.setData({
  //         appList: res.data.data.appList,
  //       })
  //       that.audioCates(that.data.currentOne);
  //     },
  //     fail: function () {
  //       console.log("获取新闻应用失败")
  //     }
  //   })
  // },


  /**
   * 获取单位APP应用 当前单位没有应用时，获取默认单位数据 fid = 117890 当只存在一个应用时，动态设置头部名称
   */
  audioApps: function () {
    var that = this;
    var encarr = [{
      wfwfid: app.globalData.fid
    }]
    var enc = app.generateEnc(encarr)
    wx.request({
      url: app.url.wxgtBaseUrl+'wechat/news/apps',
      data: {
        wfwfid: app.globalData.fid,
        enc: enc
      },
      success: function (res) {
        that.setData({
          appList: res.data.data.appList,
        })
        //判断是否有返回值，有： 查询分类，并且设置头部   没有： 查询默认单位数据
        if(res.data.data.appList.length == 0){
          //当没有应用时不显示新闻
          // that.newsDefaultApps();
        }else if(res.data.data.appList.length == 1){         
          // that.setData({
          //   currentOne: res.data.data.appList[0].appid
          // })
          //显示唯一一个应用名称 
          wx.setNavigationBarTitle({
            title: res.data.data.appList[0].appname,
          })
          //当只有一个应用的情况下  查询是否存在分类 有： 展示私有数据  没有： 展示默认数据
          that.audioCates(res.data.data.appList[0].appid);
          console.log(111)
        }else{
          //大于1个应用的情况下加载私有数据
          // console.log(that.data.currentOne,'...that.data.currentOne')
          // that.audioCates(that.data.currentOne);
          that.audioCates(7666);

          console.log(222)

        }
      },
      fail: function () {
        console.log("获取新闻应用失败")
      }
    })
  },

  /**
   * 获取默认单位数应用据
   */
  newsDefaultApps: function () {
    var that = this;
    that.setData({
      fid: 117890
    })
    var encarr = [{
      wfwfid: 117890
    }]
    var enc = app.generateEnc(encarr)
    wx.request({
      url: app.url.wxgtBaseUrl+'wechat/news/apps',
      data: {
        wfwfid: 117890,
        enc: enc
      },
      success: function (res) {
        that.setData({
          appList: res.data.data.appList,
        })
        //判断是否有返回值，有： 查询分类，并且设置头部
        if(res.data.data.appList.length != 0){
          that.setData({
            currentOne: res.data.data.appList[0].appid
          })
          that.newsDefaultCates(res.data.data.appList[0].appid);
          if(res.data.data.appList.length == 1){
            wx.setNavigationBarTitle({
              title: res.data.data.appList[0].appname,
            })
          }
        }
      },
      fail: function () {
        console.log("获取新闻应用失败")
      }
    })
  },

  //获取当前单位新闻分类
  audioCates: function (cateId) {
    var that = this;
    var encarr = [{
      wfwfid: that.data.fid
    }, {
      appid: cateId
    }]
    var enc = app.generateEnc(encarr)
    wx.request({
      url: app.url.wxgtBaseUrl+'wechat/news/cates',
      data: {
        // wfwfid: app.globalData.fid,
        wfwfid: that.data.fid,
        appid: cateId,
        enc: enc
      },
      success: function (res) {
        //需要判断是否有分类
        if(res.data.data.typeList.length != 0){
          that.setData({
            classifyArr: res.data.data.typeList,
            currentOne: cateId
          })
          if(!that.data.isfirst){
            that.setData({
              currentTwo:res.data.data.typeList[0].typeid
            }) 
          }
          that.audios();
        }else{
          //当前单位不存在分类数据时，重新获取默认单位的应用以及分类
          if(that.data.appList.length <= 1){
            that.newsDefaultApps();
          }
        }
        
      },
      fail: function () {
        
      }
    })
  },

   /**
    * 获取默认单位下的新闻分类资源
    */
   newsDefaultCates: function (cateId) {
    var that = this;
    var encarr = [{
      wfwfid: that.data.fid
    }, {
      appid: cateId
    }]
    var enc = app.generateEnc(encarr)
    wx.request({
      url: app.url.wxgtBaseUrl+'wechat/news/cates',
      data: {
        wfwfid: that.data.fid,
        appid: cateId,
        enc: enc
      },
      success: function (res) {
        //需要判断是否有分类
        if(res.data.data.typeList.length != 0){
          that.setData({
            classifyArr: res.data.data.typeList,
          })
          if(!that.data.isfirst){
            that.setData({
              currentTwo:res.data.data.typeList[0].typeid
            }) 
          }
          that.audios();
        }else{
          console.log('默认单位下不存在分类资源');
        }
      },
      fail: function () {
        
      }
    })
  },

  //根据分类获取资源
  audios: function () {
    console.log("列表列表列表")
    var that = this;
    var encarr = [{
      wfwfid: that.data.fid
    }, {
      appid: that.data.currentOne
    }, {
      typeid: that.data.currentTwo
    }, {
      current: this.data.pageNum
    }, {
      pageSize: 12
    }]
    var enc = app.generateEnc(encarr)
    wx.request({
      url: app.url.wxgtBaseUrl+'wechat/news/getListByTypeId',
      data: {
        // wfwfid: app.globalData.fid,
        wfwfid: that.data.fid,
        appid: that.data.currentOne,
        typeid: that.data.currentTwo,
        current: this.data.pageNum,
        pageSize: 12,
        enc: enc
      },
      success: function (res) {
        console.info(res.data.data)
        if (res.data.data.news.length > 0 && res.data.data.news.length == 12) {
          that.setData({
            audios: that.data.audios.concat(res.data.data.news),
            pageNum: that.data.pageNum + 1
          })
        } else if (res.data.data.news.length > 0 && res.data.data.news.length < 12) {
          that.setData({
            audios: that.data.audios.concat(res.data.data.news),
            pageNum: that.data.pageNum + 1,
            isOver: true
          })
        }

      },
      fail: function () {
        console.log("获取新闻资源失败");
      }
    })
  },

  /**
   * 点击新闻跳转逻辑
   */
  navigatorNews: function(e){
    console.log(e);
    if(e.currentTarget.dataset.type == 0){
      wx.navigateTo({
        url: '/pages/newsInfo/newsInfo?infoid='+e.currentTarget.dataset.infoid+'&appid=' + e.currentTarget.dataset.appid,
      })
    }else if(e.currentTarget.dataset.type == 1){
      wx.navigateTo({
        url: '/pages/link/link?url=' + encodeURIComponent(e.currentTarget.dataset.content),
      })
    }else{
      wx.navigateTo({
        url: e.currentTarget.dataset.content
      })
    }
  }

  


})